********************************************************************************
****				 Replication Table A.8 & Figure A.9	        			****
****	Simultaneous equation models (Two-stage probit least squares)		****
********************************************************************************

* Preparations
macro drop _all 
capture log close 
clear all 
drop _all 
log using replication_stata_A8.log, replace text 

// The purpose of this .do file is to replicate Table A.8 and Fig. A.8 in the Appendix. 
// We run the two-stage estimation method for simultaneous equations models with
// the cdsimeq command written by O. Keshk.


version 16 // Stata version used

set more off, permanently 

set linesize 255 

********************************************************************************
****				Read and prepare data (Model 20)		        		****
********************************************************************************

* Set working directory if neccessary
cd /Users/xhelse/Documents/Research/Papers/protest_issues_final/Replication/

use "data_subset_leader_ord.dta", clear // load subsample of MMAD data

* take log of selected variables
generate participants_log = ln(avg_participants)
generate gdp_log = ln(gdppc)
generate pop_log = ln(pop)

* create dummy variables for selected variables (year, violence, scope)
tabulate year, gen(year_dummy)
tabulate part_violence, gen(violence_dummy)
tabulate scope, gen(scope_dummy)

********************************************************************************
****			  Run model and save estimates (Model 20)	        	    ****
********************************************************************************

* Run model
cdsimeq(issue_leader repr_hist_country_21 unrepr_hist_country_21 gdp_log pop_log v2x_polyarchy theta_mean v2x_ex_military v2x_ex_party armed_confl year_dummy1-year_dummy12)(repression_binary issue_performance issue_rat_legal issue_regime issue_repression violence_dummy2-violence_dummy4 scope_dummy2-scope_dummy3 participants_log gdp_log pop_log v2exl_legitperf v2exl_legitratio v2exl_legitlead year_dummy1-year_dummy12), estimates_hold

* Remove estimates that are not needed
_estimates unhold model_2

* save regression output to Latex
esttab using table1.tex, se ar2 b(a2) replace

* Calculate marginal effects
margins, at(I_issue_leader=(0(.05)1)) atmeans vsquish post expression(normal(xb()))

* Save marginal effects
est store model1_1


********************************************************************************
****				Read and prepare data (Model 21)		        		****
********************************************************************************

* Set working directory if neccessary
cd /Users/xhelse/Documents/Research/Papers/protest_issues_final/Replication/

use "data_subset_noleader_ord.dta", clear // load subsample of MMAD data

* take log of selected variables
generate participants_log = ln(avg_participants)
generate gdp_log = ln(gdppc)
generate pop_log = ln(pop)

* create dummy variables for selected variables (year, violence, scope)
tabulate year, gen(year_dummy)
tabulate part_violence, gen(violence_dummy)
tabulate scope, gen(scope_dummy)

********************************************************************************
****			  Run model and save estimates (Model 21)	        	    ****
********************************************************************************

_estimates unhold model_1

cdsimeq(issue_leader repr_hist_country_21 unrepr_hist_country_21 gdp_log pop_log v2x_polyarchy theta_mean v2x_ex_military v2x_ex_party armed_confl year_dummy1-year_dummy12)(repression_binary issue_performance issue_rat_legal issue_regime issue_repression violence_dummy2-violence_dummy4 scope_dummy2-scope_dummy3 participants_log gdp_log pop_log v2exl_legitperf v2exl_legitratio v2exl_legitlead year_dummy1-year_dummy12), estimates_hold

* Remove estimates that are not needed
_estimates unhold model_2

* save regression output to Latex
esttab using table2.tex, se ar2 b(a2) replace

* Calculate marginal effects
margins, at(I_issue_leader=(0(.05)1)) atmeans vsquish post expression(normal(xb()))

* Save estimates
est store model2_1

********************************************************************************
****			  Plot marginal effects (Fig. A.8)	  						****
********************************************************************************

set scheme lean2

* Plot marginal effects 
coefplot ///
(model1_1, recast(line) lpattern(solid) lcolor(black))  ///
(model2_1, recast(line) lpattern(dash) lcolor(black))  ///
, at sort plotlabels("Strong leader claims" "Weak leader claims") xtitle(Leader issue) ytitle(Pr(Repression)) ///
ciopts(lcolor(gray) color(%70) recast(rarea)) legend(pos(6) col(2))

********************************************************************************
********								END								  ******
********************************************************************************

log close




